#define _XOPEN_SOURCE
//#include <unistd.h>
//#include <shadow.h>
//#include <hunc.h>
#include "../../include/myfunc.h"
//makefile里gcc链接时要加 -lcrypt

void get_salt(char *salt, char *passwd)//获得盐值
{
    int i, j;
    for(i = 0, j = 0; passwd[i] && j != 3; ++i)
    {
        if(passwd[i] == '$')
        {
            ++j;
        }
    }
    strncpy(salt, passwd, i - 1);
}
int passwd_judge(char* login_name, char* login_passwd)//验证密码，正确返回1，错误返回0
{
    struct spwd *sp;
    char salt[512] = {0};
    sp = getspnam(login_name);
    get_salt(salt, sp->sp_pwdp);
    if(strcmp(sp->sp_pwdp, crypt(login_passwd, salt)) == 0)
    {
        //printf("验证通过\n");
        return 1;
    }else{
        //printf("验证失败\n");
        return 0;
    }
}